package com.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.domain.pojo.Orders;
import com.hospital.domain.utils.Result;
import com.hospital.mapper.OrdersMapper;
import com.hospital.service.OrdersService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Override
    public Result<List<Orders>> listAll(Integer status) {
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        if (status != null) qw.eq(Orders::getPayStatus, status);
        qw.orderByDesc(Orders::getCreateTime);
        List<Orders> list = this.list(qw);
        return Result.ok(list);
    }

    @Override
    public Result<List<Orders>> listByPatient(Integer pId) {
        if (pId == null) return Result.fail("缺少患者ID");
        List<Orders> list = this.lambdaQuery().eq(Orders::getPId, pId).orderByDesc(Orders::getCreateTime).list();
        return Result.ok(list);
    }

    @Override
    @Transactional
    public Result<Void> pay(Integer orderId) {
        Orders o = this.getById(orderId);
        if (o == null) return Result.fail("订单不存在");
        if (o.getPayStatus() != null && o.getPayStatus() == 1) return Result.fail("订单已支付");
        if (o.getPayStatus() != null && o.getPayStatus() == 2) return Result.fail("订单已退款");
        o.setPayStatus(1);
        o.setPayTime(LocalDateTime.now());
        this.updateById(o);
        return Result.ok();
    }

    @Override
    @Transactional
    public Result<Void> refund(Integer orderId) {
        Orders o = this.getById(orderId);
        if (o == null) return Result.fail("订单不存在");
        if (o.getPayStatus() == null || o.getPayStatus() == 0) return Result.fail("未支付订单无法退款");
        if (o.getPayStatus() == 2) return Result.fail("已退款");
        o.setPayStatus(2);
        o.setRefundTime(LocalDateTime.now());
        this.updateById(o);
        return Result.ok();
    }

    @Override
    @Transactional
    public Result<Integer> createDepositOrder(Integer pId, Integer aId) {
        if (pId == null || aId == null) return Result.fail("参数缺失");
        // 避免重复创建
        long exists = this.lambdaQuery().eq(Orders::getSourceType, 1).eq(Orders::getSourceId, aId).count();
        if (exists > 0) return Result.ok(0);
        Orders o = new Orders();
        o.setPId(pId);
        o.setItem("住院押金");
        o.setAmount(new BigDecimal("5000.00"));
        o.setPayStatus(0);
        o.setSourceType(1);
        o.setSourceId(aId);
        o.setCreateTime(LocalDateTime.now());
        this.save(o);
        return Result.ok(o.getOrderId());
    }

    @Override
    @Transactional
    public Result<Integer> createDrugOrder(Integer pId, Integer purchaseId, String itemTitle, BigDecimal amount) {
        if (pId == null || purchaseId == null) return Result.fail("参数缺失");
        long exists = this.lambdaQuery().eq(Orders::getSourceType, 2).eq(Orders::getSourceId, purchaseId).count();
        if (exists > 0) return Result.ok(0);
        Orders o = new Orders();
        o.setPId(pId);
        o.setItem(itemTitle != null && !itemTitle.isEmpty() ? itemTitle : "药品费用");
        o.setAmount(amount == null ? BigDecimal.ZERO : amount);
        o.setPayStatus(0);
        o.setSourceType(2);
        o.setSourceId(purchaseId);
        o.setCreateTime(LocalDateTime.now());
        this.save(o);
        return Result.ok(o.getOrderId());
    }
}

